package com.orangelife.mobile.voucher.biz;

import android.content.Context;
import android.widget.TextView;

import com.curry.android.util.DecimalUtil;
import com.curry.android.util.MoneyUtil;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.main.biz.JumpUtil;
import com.orangelife.mobile.util.ToastHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 维护抵用券的业务逻辑，目前此类可维护的多个抵用券列表（多商户订单），也包括只有一个抵用券列表的情况（单个商品或服务的订单）；每个抵用券列表可多选（叠加抵用券）或单选（只能选一张）
 * Created by wgf on 2016/2/26.
 *
 * @author wgf
 * @since 4.3.2
 */
public class VoucherBiz {
    public static final String TYPE = "type";
    public static final String USE_TYPE = "useType";
    public static final String STR_NO_VOUCHER = "暂无";

    public static void test() {

    }

    public static void jumpToVoucherRule(Context context) {
        if (context != null) {
            JumpUtil.jumpActivity(context, context.getString(R.string.server_url) + "/orangelife_web/views/orangelife/prop/activityth/rule.html?title=规则说明");
        }
    }


    /** include layout="@layout/include_voucher */
    public static void setVoucherInfoBackground(TextView tvVoucher, String voucherInfo, Context context) {
        tvVoucher.setText(voucherInfo);
        if (STR_NO_VOUCHER.equals(voucherInfo)) {/*暂无*/
            tvVoucher.setTextColor(context.getResources().getColor(R.color.white));
            tvVoucher.setBackgroundResource(R.drawable.dot_voucher_gray);
        } else {/*抵用券金额||未使用抵用券*/
            tvVoucher.setTextColor(context.getResources().getColor(R.color.TextBlack));
            tvVoucher.setBackgroundResource(0);
        }
    }

    public static String getVoucherInfo(String oldVoucherInfo) {
        return getVoucherInfo(oldVoucherInfo, SelectedVoucherIDListHelper.getInstance().getTotalSelectedVoucherPrice());
    }

    public static String getVoucherInfo(String oldVoucherInfo, String totalSelectedVoucherPrice) {
        ArrayList<String> selfVoucherList = SelectedVoucherIDListHelper.getInstance().getSelfVoucherList();
        /*默认是“暂无”*/
        String newVoucherInfo = oldVoucherInfo;
        /*使用抵用券列表为空，有两种情况，一是原来就是“暂无”，这种情况是不可能点进去的，二是取消了所有抵用券，这时需要显示抵用券数量*/
        /*如果未选择抵用券，只是请求了最高金额抵用券，则不能使用此方法生成抵用券信息，因为选中的抵用券肯定是空的*/
        if (selfVoucherList == null || selfVoucherList.size() == 0 || StringUtil.isBlank(totalSelectedVoucherPrice)) {/*不使用抵用券，告诉用户有几张可用抵用券(不是已选择的，而是全部)*/
            if (VoucherBiz.hasVoucher(newVoucherInfo, false)) {
                newVoucherInfo = "未使用抵用券";
            }
        } else {/*选中了1到多张抵用券，需要计算新的抵用券总价*/
            totalSelectedVoucherPrice = MoneyUtil.handleMoney(totalSelectedVoucherPrice);
            newVoucherInfo = totalSelectedVoucherPrice + "元抵用券";
        }
        return newVoucherInfo;
    }

    public static boolean hasVoucher(String voucherInfo, boolean isShow) {
        if (StringUtil.isBlank(voucherInfo) || STR_NO_VOUCHER.equals(voucherInfo)) {
            if (isShow) {
                ToastHelper.getInstance()._toast("暂无抵用券");
            }
            return false;
        }
        return true;
    }

    /**
     * 因为选中和取消选中抵用券，如果直接在正式的选中列表修改，需要计算很多，性能很低，所以在Adapter中使用临时列表tempSelectedVoucherList保存选中状态，在结束的时候，将状态更新到正式的选中列表selectedVouchersIDListMap
     *
     * @param tempVoucherSelectedStatusList 临时的选中列表
     */
    public static ArrayList<String> getNewSelectedVoucherIDList(List<Map<String, Object>> tempVoucherSelectedStatusList) {
        /*使用静态方法，是因为这个方法和单例所要维护的集合无关，不需要放在单例中*/
        if (tempVoucherSelectedStatusList == null) {
            return null;
        }
        ArrayList<String> newSelectedVoucherList = new ArrayList<>();
        for (int i = 0; i < tempVoucherSelectedStatusList.size(); i++) {
            Map<String, Object> voucherInfoMap = tempVoucherSelectedStatusList.get(i);
            if ("true".equals(String.valueOf(voucherInfoMap.get("hasSelected")))) {
                String voucherID = String.valueOf(voucherInfoMap.get("couponID"));
                newSelectedVoucherList.add(voucherID);
            }
        }
        return newSelectedVoucherList;

    }

    /**
     * 给抵用券列表添加是否被选中的参数。
     * （日后可能会考虑加入下拉刷新=重新初始化选中状态，上拉加载=增加选中状态，新增的，默认）
     *
     * @param tempVoucherListWithSelectedStatus 临时保存选中状态的列表，目前直接使用抵用券列表保存
     * @return
     */
    public static List<Map<String, Object>> initTempVoucherListWithSelectedStatus(List<Map<String, Object>> tempVoucherListWithSelectedStatus, ArrayList<String> selectedVoucherList) {
        if (tempVoucherListWithSelectedStatus == null || selectedVoucherList == null) {
            return null;
        }
        for (int j = 0; j < tempVoucherListWithSelectedStatus.size(); j++) {
            Map<String, Object> voucherInfo = tempVoucherListWithSelectedStatus.get(j);
            String voucherID = String.valueOf(voucherInfo.get("couponID"));
            String hasSelected = "false";
            for (int i = 0; i < selectedVoucherList.size(); i++) {
                String selectedVoucherID = selectedVoucherList.get(i);
                if (voucherID.equals(selectedVoucherID)) {
                    /*初始化抵用券叠加状态信息，目前抵用券已取消叠加属性，全部不可叠加*/
//                    VoucherBiz.getInstance().checkVoucherSuperPositionStatusWhenSelecting(type, useType, voucherID);
                    /*初始化选中状态*/
                    hasSelected = "true";
                    break;
                }
            }
            tempVoucherListWithSelectedStatus.get(j).put("hasSelected", hasSelected);
        }
        return tempVoucherListWithSelectedStatus;
    }


    /**
     * 更新临时抵用券选中状态列表的项的选中状态
     *
     * @param tempSelectedVoucherList 临时抵用券选中状态列表
     * @param pos                     选中的项
     * @param newHasSelected          新的选中状态
     * @param isSingleSelectMode      单选模式或多选模式
     */
    public static void updateTempVoucherSelectedStatus(List<Map<String, Object>> tempSelectedVoucherList, int pos, boolean newHasSelected, boolean isSingleSelectMode) {
        tempSelectedVoucherList.get(pos).put("hasSelected", String.valueOf(newHasSelected));
        if (isSingleSelectMode && newHasSelected) {/*如果是单选模式，且是勾选抵用券，则需要清除其他抵用券的勾选态*/
            for (int i = 0; i < tempSelectedVoucherList.size(); i++) {
                if (i != pos) {
                    tempSelectedVoucherList.get(i).put("hasSelected", false);
                }
            }
        }
    }

    /** 计算选中的抵用券的总金额 */
    public static String getTotalSelectedVoucherPrice(List<Map<String, Object>> tempSelectedVoucherList) {
        String totalSelectedVoucherPrice = "0";
        for (int j = 0; j < tempSelectedVoucherList.size(); j++) {
            boolean hasSelected = Boolean.parseBoolean(String.valueOf(tempSelectedVoucherList.get(j).get("hasSelected")));
            if (hasSelected) {
                /*抵用券金额*/
                String price = String.valueOf(tempSelectedVoucherList.get(j).get("couponValue"));
                totalSelectedVoucherPrice = DecimalUtil.add(totalSelectedVoucherPrice, price);
            }
        }
        return totalSelectedVoucherPrice;
    }

}
